{% extends "base.html" %}

{% block title %}首页{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 系统概览 -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-title mb-0">监控点数量</h6>
                            <h2 class="mt-2 mb-0">{{ stats.points_count }}</h2>
                        </div>
                        <i class="fas fa-map-marker-alt fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-title mb-0">今日数据量</h6>
                            <h2 class="mt-2 mb-0">{{ stats.today_data_count }}</h2>
                        </div>
                        <i class="fas fa-database fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-title mb-0">活跃报警</h6>
                            <h2 class="mt-2 mb-0">{{ stats.active_alarms }}</h2>
                        </div>
                        <i class="fas fa-bell fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-title mb-0">在线用户</h6>
                            <h2 class="mt-2 mb-0">{{ stats.user_count }}</h2>
                        </div>
                        <i class="fas fa-users fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 监控点状态 -->
    <div class="row mb-4">
        <div class="col">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">监控点状态</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>监控点</th>
                                    <th>位置</th>
                                    <th>溶解氧(mg/L)</th>
                                    <th>pH值</th>
                                    <th>温度(℃)</th>
                                    <th>氨氮(mg/L)</th>
                                    <th>浊度(NTU)</th>
                                    <th>最后更新</th>
                                    <th>状态</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for point in points %}
                                <tr>
                                    <td>{{ point.name }}</td>
                                    <td>{{ point.location }}</td>
                                    <td>{{ point.latest_data.dissolved_oxygen if point.latest_data else '-' }}</td>
                                    <td>{{ point.latest_data.ph_value if point.latest_data else '-' }}</td>
                                    <td>{{ point.latest_data.temperature if point.latest_data else '-' }}</td>
                                    <td>{{ point.latest_data.ammonia_nitrogen if point.latest_data else '-' }}</td>
                                    <td>{{ point.latest_data.turbidity if point.latest_data else '-' }}</td>
                                    <td>{{ point.latest_data.timestamp.strftime('%Y-%m-%d %H:%M:%S') if point.latest_data else '-' }}</td>
                                    <td>
                                        {% if point.latest_data and (now - point.latest_data.timestamp).total_seconds() < 300 %}
                                        <span class="badge bg-success">在线</span>
                                        {% else %}
                                        <span class="badge bg-danger">离线</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 最新报警 -->
    <div class="row">
        <div class="col">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">最新报警</h5>
                    <a href="{{ url_for('alarm.index') }}" class="btn btn-primary btn-sm">查看全部</a>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>监控点</th>
                                    <th>参数</th>
                                    <th>当前值</th>
                                    <th>阈值</th>
                                    <th>级别</th>
                                    <th>时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for alarm in alarms %}
                                <tr>
                                    <td>{{ alarm.point.name }}</td>
                                    <td>{{ alarm.parameter }}</td>
                                    <td>{{ alarm.current_value }}</td>
                                    <td>{{ alarm.threshold }}</td>
                                    <td>
                                        <span class="badge bg-{{ 'warning' if alarm.level == 'warning' else 'danger' }}">
                                            {{ '警告' if alarm.level == 'warning' else '危险' }}
                                        </span>
                                    </td>
                                    <td>{{ alarm.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td colspan="6" class="text-center">暂无报警信息</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 